/**
 * 文件名：SecurityInterceptor.java
 *
 * 创建人：虞云波 - .com
 *
 * 创建时间：2017年10月9日 下午3:57:05
 *
 * 版权所有：江苏晟邦网络科技有限公司
 */
package com.cbwl.eoas.cms.web.framework.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.cbwl.eoas.cms.api.sysconfig.ISysLogService;
import com.cbwl.eoas.cms.model.sysconfig.SysLogDTO;
import com.cbwl.eoas.cms.util.RequestLogUtil;
import com.cbwl.eoas.common.cache.redis.RedisCache;
import com.cbwl.eoas.common.file.ExceptionPropertiesUtil;
import com.cbwl.eoas.common.framework.session.SessionManager;
import com.cbwl.eoas.common.framework.utils.ExceptionUtils;
import com.cbwl.eoas.common.util.constain.DicDetailConstant;

/**
 * 
 * <p>[描述信息：cms系统日志拦截器]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年10月30日 上午9:51:58
 */
public class SysLogInterceptor extends HandlerInterceptorAdapter{

	
	private static Logger log = LoggerFactory.getLogger(SysLogInterceptor.class);
	
	@Autowired
	private RedisCache<String,String> redisCache;
	
	
	@Autowired
	private ISysLogService isysLogService;
	
	
	/**
	 * 
	 * <p></p>
	 * 
	 * @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年10月30日 上午9:51:47
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
		
		
		SysLogDTO bean = RequestLogUtil.getRequestSysLog(request);
		
		bean.setGmtCreate(new Date());
		bean.setCreater(SessionManager.getCurrentUser()==null?null:SessionManager.getCurrentUser().getId());
		
		
		if(ex !=null){
			bean.setSysResResult(DicDetailConstant.sysresresult_2);
			bean.setSysExceptionInfo(ExceptionUtils.getTrace(ex).length()>512?ExceptionUtils.getTrace(ex).substring(0, 512):ExceptionUtils.getTrace(ex));
		}else{
			bean.setSysResResult(DicDetailConstant.sysresresult_1);
		}
		isysLogService.saveObject(bean);
		
	}








	public static void main(String[] args) {
		System.out.println(ExceptionPropertiesUtil.getExceptionDesc("CBWL-000004"));
	}
	
}
